package com.xuetang9.t9.xinhuaxia.controller;

import com.github.pagehelper.PageInfo;
import com.xuetang9.t9.xinhuaxia.beans.Goods;
import com.xuetang9.t9.xinhuaxia.beans.GoodsPicturepath;
import com.xuetang9.t9.xinhuaxia.service.IGoodsPicturepathService;
import com.xuetang9.t9.xinhuaxia.service.IGoodsService;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李亚奇
 * @version 1.0
 * @desc TODO
 * @date 2020/2/23 18:59
 * @copyright liyq
 * @address 成都西部国际金融中心2栋2201
 **/
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsPicturepathService goodsPicturepathService;

    private GoodsPicturepath goodsPicturepath;

    @RequestMapping(value = "save", method= RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public boolean insertGoods(HttpServletRequest request, Goods goods){
        System.out.println(goods);
        boolean insertGoods = goodsService.insertGoods(goods);
        Goods newGoods = goodsService.queryByGoods(goods);
        System.out.println(newGoods);
        /*保存图片*/
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/");
        System.out.println("路径显示:");
        System.out.println(realPath);
        String savePath = realPath + "/backstage/goodspicture";
        System.out.println(savePath);
        File saveFile = new File(savePath);
        if (!saveFile.exists()){
            saveFile.mkdir();
        }
        MultipartFile[] files = goods.getGoodsPicFiles();
        if (null!=files&&files.length>0){
            System.out.println("文件个数："+files.length);
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                if (null != file){
                    String picPath = savePath + "/" + file.getOriginalFilename();
                    System.out.println(picPath);
                    File realFile = new File(picPath);
                    try {
                        Files.copy(file.getInputStream(),realFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        String goodsPicPath = "/backstage/goodspicture/" + file.getOriginalFilename();
                        goodsPicturepath = new GoodsPicturepath();
                        goodsPicturepath.setGpGoodsid(newGoods.getGoodsid());
                        goodsPicturepath.setGpPicpath(goodsPicPath);
                        boolean insert = goodsPicturepathService.insert(goodsPicturepath);
                        if (insert == false){
                            return false;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
        }
        return insertGoods;
    }
    @GetMapping("remove/{id}")
    public boolean removeGoods(@PathVariable(value = "id",required = false) Long id){
        boolean removeGoods = goodsService.removeGoods(id);
        System.out.println(removeGoods);
        return removeGoods;
    }
    @PostMapping("update")
    public boolean updateGoods(@RequestBody Goods goods){
        System.out.println(goods);
        boolean updateGoods = goodsService.updateGoods(goods);
        return updateGoods;
    }

    @PostMapping("queryById/{id}")
    public Goods queryById(@PathVariable(value = "id",required = false) Long id){
        return goodsService.queryById(id);
    }

    @PostMapping("queryByGoods")
    public Goods queryByGoods(@RequestBody Goods goods){
        return goodsService.queryByGoods(goods);
    }

    @GetMapping("list/{page}/{perPagesize}")
    public PageInfo<Goods> queryByPage(@PathVariable(value = "page",required = false) Integer page,@PathVariable(value = "perPagesize",required = false) Integer perPagesize){
        PageInfo<Goods> pageInfo = goodsService.queryByPage(page, perPagesize);
        return pageInfo;
    }

    @RequestMapping(value = "insert", method= RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public boolean insert(HttpServletRequest request,Goods goods){
        System.out.println("数据测试");
        System.out.println(goods);
        MultipartFile[] picFiles = goods.getGoodsPicFiles();
        if(null!=picFiles){
            System.out.println(55+"长度为："+picFiles.length);
            for (int i = 0; i < picFiles.length; i++) {
                System.out.println(picFiles[i]);
            }
        }
        System.out.println(55);
        return false;
    }


    @RequestMapping("/queryByLabel/{page}/{perPagesize}")
    public PageInfo<Goods> queryByLabel(String label,@PathVariable(value = "page",required = false) Integer page,@PathVariable(value = "perPagesize",required = false) Integer perPagesize){
        PageInfo<Goods> pageInfo = goodsService.queryByLabel(label, page, perPagesize);
        return pageInfo;
    }

    @RequestMapping("/queryshopmain")
    public List<Goods> queryShopMain(String label){
        List<Goods> shopgood = goodsService.queryShopMain(label);
        return shopgood;
    }

    @RequestMapping("/queryall")
    public List<Goods> queryAll(String label){
        List<Goods> shopgood = goodsService.queryAll();
        return shopgood;
    }

    @RequestMapping("/querybyFirstCategory")
    public List<Goods> querybyFirstCategory(Long firstCategoryid){
        List<Goods> shopgood = goodsService.queryByFirstCategory(firstCategoryid);
        return shopgood;
    }
}
